package com.ruoyi.project.propertySystem.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.configureSystem.domain.VStation;
import com.ruoyi.project.configureSystem.mapper.VStationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.propertySystem.mapper.StationInspectFormworkMapper;
import com.ruoyi.project.propertySystem.domain.StationInspectFormwork;
import com.ruoyi.project.propertySystem.service.IStationInspectFormworkService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 站点巡检模板Service业务层处理
 * 
 * @author zcy
 * @date 2021-01-18
 */
@Service
public class StationInspectFormworkServiceImpl implements IStationInspectFormworkService 
{
    @Autowired
    private StationInspectFormworkMapper stationInspectFormworkMapper;
    @Autowired
    private VStationMapper vStationMapper;

    /**
     * 查询站点巡检模板
     * 
     * @param id 站点巡检模板ID
     * @return 站点巡检模板
     */
    @Override
    public StationInspectFormwork selectStationInspectFormworkById(Long id)
    {
        return stationInspectFormworkMapper.selectStationInspectFormworkById(id);
    }

    /**
     * 查询站点巡检模板列表
     * 
     * @param stationInspectFormwork 站点巡检模板
     * @return 站点巡检模板
     */
    @Override
    public List<StationInspectFormwork> selectStationInspectFormworkList(StationInspectFormwork stationInspectFormwork)
    {
        if(stationInspectFormwork.getIsStation()!=null){
            List<Long> stationIds = new ArrayList<>();
            if(stationInspectFormwork.getIsStation()==0){
                //区域，则查询下面所有的站点
                stationIds =  vStationMapper.selectVStationIdsByAreaId(stationInspectFormwork.getAreaId());
                if(stationIds.size()==0){
                    return new ArrayList<>();
                }
            }else {
                VStation vStation = vStationMapper.selectVStationByStationCode(stationInspectFormwork.getStationcode());
                stationIds.add(vStation.getId());
            }
            if(stationIds.size()==0){
                return new ArrayList<>();
            }
                stationInspectFormwork.setStationIds(stationIds);
        }
        return stationInspectFormworkMapper.selectStationInspectFormworkList(stationInspectFormwork);
    }

    /**
     * 新增站点巡检模板
     * 
     * @param stationInspectFormwork 站点巡检模板
     * @return 结果
     */
    @Override
    @Transactional
    public int insertStationInspectFormwork(StationInspectFormwork stationInspectFormwork)
    {
        //厂站编号获取厂站id
        VStation vStation = vStationMapper.selectVStationByStationCode(stationInspectFormwork.getStationcode());
        stationInspectFormwork.setStationId(vStation.getId());
        stationInspectFormwork.setCreateBy(SecurityUtils.getUsername());
        stationInspectFormwork.setGmtCreate(new Date());
        return stationInspectFormworkMapper.insertStationInspectFormwork(stationInspectFormwork);
    }

    /**
     * 修改站点巡检模板
     * 
     * @param stationInspectFormwork 站点巡检模板
     * @return 结果
     */
    @Override
    @Transactional
    public int updateStationInspectFormwork(StationInspectFormwork stationInspectFormwork)
    {
        //厂站编号获取厂站id
        VStation vStation = vStationMapper.selectVStationByStationCode(stationInspectFormwork.getStationcode());
        stationInspectFormwork.setStationId(vStation.getId());
        return stationInspectFormworkMapper.updateStationInspectFormwork(stationInspectFormwork);
    }

    /**
     * 批量删除站点巡检模板
     * 
     * @param ids 需要删除的站点巡检模板ID
     * @return 结果
     */
    @Override
    public int deleteStationInspectFormworkByIds(Long[] ids)
    {
        return stationInspectFormworkMapper.deleteStationInspectFormworkByIds(ids);
    }

    /**
     * 删除站点巡检模板信息
     * 
     * @param id 站点巡检模板ID
     * @return 结果
     */
    @Override
    public int deleteStationInspectFormworkById(Long id)
    {
        return stationInspectFormworkMapper.deleteStationInspectFormworkById(id);
    }
}
